package com.itany.shop.service.proxy;

import com.itany.shop.entity.Product;
import com.itany.shop.exception.ServiceException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.ProductService;
import com.itany.shop.tran.TransactionManager;
import com.itany.shop.util.PageInfo;

import java.util.List;

/**
 * @Author:石小俊
 * @Date:2022/11/16 11:18
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
public class ProductServiceProxy implements ProductService {

    private TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productServiceTarget");

    @Override
    public List<Product> findAll() {
        try {
            tran.begin();
            List<Product> products = productService.findAll();
            tran.commit();
            return products;
        } catch (Exception e) {
            tran.rollback();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public List<Product> findPage(Integer pageNo, Integer pageSize) {
        try {
            tran.begin();
            List<Product> products = productService.findPage(pageNo,pageSize);
            tran.commit();
            return products;
        } catch (Exception e) {
            tran.rollback();
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public PageInfo<Product> findPageInfo(Integer pageNo, Integer pageSize) {
        try {
            tran.begin();
            PageInfo<Product> productPage = productService.findPageInfo(pageNo,pageSize);
            tran.commit();
            return productPage;
        } catch (Exception e) {
            tran.rollback();
            throw new ServiceException(e.getMessage());
        }
    }
}
